<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户</title>
<style>
.form-horizontal {
	position: relative;
	top: 20px;
	left: 50px;
	width: 90%;
}
</style>
</head>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-theme.min.css">
<script src="${pageContext.request.contextPath}/js/jquery-1.11.1.js"></script>
<script
	src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/bootstrap/js/bootstrapValidator.min.js"></script>
<body>
	<nav class="navbar navbar-inverse navbar-fixed-top">
	<div class="container">
		<div class="navbar-header">
			<a class="navbar-brand" href="Javascript: void(0)"><strong
				class="h3">添加用户&nbsp;&nbsp;</strong></a>
		</div>
	</div>
	</nav>
	<div id="change_user" class="container"
		style="position: relative; top: 100px; left: 100px">
		<form id="myform" action="" method="post"
			onsubmit="script: return checkForm()">
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>新用户</legend>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="username">用户名</label>
						<div class="col-sm-4">
							<input class="form-control" id="username" name="username"
								type="text" placeholder="" />
						</div>
					</div>
					<!-- <div class="form-group">
						<label class="col-sm-2 control-label" for="password">密码</label>
						<div class="col-sm-4">
							<input class="form-control" id="password" name="password"
								type="password" placeholder=" " />
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="confirmpassword">确认密码</label>
						<div class="col-sm-4">
							<input class="form-control" id="confirmpassword"
								name="confirmpassword" type="password" placeholder=" " />
						</div>
					</div> -->
					<div class="form-group">
						<label class="col-sm-2 control-label" for="email">邮箱</label>
						<div class="col-sm-4">
							<input class="form-control" id="email" name="email" type="text"
								placeholder=" " />
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="phonenumber">电话</label>
						<div class="col-sm-4">
							<input class="form-control" id="phonenumber" name="phonenumber"
								type="text" placeholder=" " />
						</div>
					</div>
				</fieldset>
			</div>
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>操作</legend>
					<div align="center">
						<input id="submit" type="button" class="btn btn-primary"
							value="提交" /> &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset"
							class="btn btn-primary" value="重置" />
					</div>
				</fieldset>
			</div>
		</form>
	</div>
</body>
<script>
	function checkForm() {
		var checkusername = /^[a-zA-Z0-9]{2,11}$/;
		var checkpassword = /^[a-zA-Z0-9]{6,11}$/;
		var checkphone = /^[0-9]{11}$/;
		var checkemail = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
		var username = $("#username").val();
		var password = $("#password").val();
		var confirmpassword = $("#confirmpassword").val();
		var email = $("#email").val();
		var phonenumber = $("#phonenumber").val();
		if (!checkusername.test(username)) {
			alert("用户名格式不正确！");
			return false;
		}
		/*if(!checkpassword.test(password)){
			alert("密码格式不正确！");
			return false;
		}
		if(password!=confirmpassword){
			alert("确认密码与密码不一致");
			return false;
		}*/
		/* if (!checkpassword.test(password) && password != "") {
			alert("密码格式不正确！");
			return false;
		}
		if (password != confirmpassword) {
			alert("确认密码与密码不一致");
			return false;
		} */

		else if ((!checkemail.test(email)) && (email != "")) {
			alert("请输入有效的邮箱");
			return false;
		} else if (!checkphone.test(phonenumber)) {
			alert("请输入有效的电话号码");
			return false;
		} else
			return true;
	}
	$(function() {
		$("#submit").click(function() {
			if (checkForm() == false) {
				return false;
			}
			var username = $("#username").val();
			var password = $("#password").val();
			var phonenumber = $("#phonenumber").val();
			var email = $("#email").val();
			if (password == "")
				password = "123456";
			$.post("UserServlet?method=addUser", {
				username : username,
				password : password,
				phonenumber : phonenumber,
				email : email
			}, function(result) {
				var result = eval('(' + result + ')');
				if (result.check == "0") {
					alert("error,请重新添加");
				} else {
					if (result.exist == "-1") {
						alert("此电话号码已被使用");
					} else if (result.exist == "0") {
						alert("插入失败！");
					} else if (result.exist == "1") {
						alert("插入成功");
						$("input[type=reset]").trigger("click");
					}
				}

			})
		})
	})
</script>
</html>